package com.zxp.server.controller;

import com.zxp.server.config.Result;
import com.zxp.server.service.FileService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Api(value = "文件上传读取", tags = "文件上传读取")
@RestController
@RequestMapping("/file")
public class FileController {

    @Autowired
    FileService fileService;

    @PostMapping("/saveFile")
    public Result SaveFile(MultipartFile file){
        String path = null;
        try {
             path = fileService.SaveFile(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Result.success("上传成功",path);
    }

    @GetMapping("/getFile/{fileName}")
    public void getFile(@PathVariable String fileName, HttpServletResponse res){
        try {
            fileService.getFile(fileName,res);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
